<?php

/**
 * Description of selectBook
 *
 * @author nahuel.lema
 * @author maxi.ya�ez
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class FaqController extends CI_Controller {
    public function categorias() {
        $results = array();
        $categorias = Doctrine::getTable('CategoriaFaq')->findAll();
        foreach ($categorias as $categoria) {
            $n = array ();
            $n['id'] = $categoria->id;
            $n['contenido'] = utf8_encode($categoria->contenido);
            $n['uri'] = $categoria->uri;
            $results[] = $n;
        }
        $results = json_encode($results);
        header("Content-type: application/json");
        echo($results);
    }
    
    public function topTen() {
        $results = array();
        $topTen = Faq::listar(array('estado'=>1, 'orderBy'=>'visitas', 'sentido'=>'desc', 'cuantos'=>'10'));
        foreach ($topTen as $faq) {
            $n = array();
            $n['id'] = $faq->id;
            $n['pregunta'] = utf8_encode($faq->pregunta);
            $n['uri'] = $faq->uri;
            $results[] = $n;
        }
        $results = json_encode($results);
        header("Content-type: application/json");
        echo($results);
    }
    
    public function preguntasRelacionadas($idPregunta) {
        $results = array();
        $cat = Doctrine::getTable('faq')->find($idPregunta)->categoria->id;
        $cuantos = rand(3, 5);
        $faqs = Faq::listar(array('categoria'=>$cat, 'estado'=>1, 'orderBy'=>'visitas', 'cuantos'=>$cuantos));
        foreach ($faqs as $faq) {
            $n = array();
            $n['id'] = $faq->id;
            $n['pregunta'] = utf8_encode($faq->pregunta);
            $n['uri'] = $faq->uri;
            $results[] = $n;
        }
        $results = json_encode($results);
        header("Content-type: application/json");
        echo($results);
    }
    
    public function selectByCategoria($cat) {
        $results = array();
        $faqs = Faq::listar(array('categoria'=>$cat, 'estado'=>1, 'orderBy'=>'pregunta'));
        foreach ($faqs as $faq) {
            $n = array();
            $n['id'] = $faq->id;
            $n['pregunta'] = utf8_encode($faq->pregunta);
            $n['uri'] = $faq->uri;
            $results[] = $n;
        }
        $results = json_encode($results);
        header("Content-type: application/json");
        echo($results);
    }
    
    public function preguntaById($id) {
        $faq = Doctrine::getTable('faq')->find($id);
        $result = array();
        $result['id'] = $faq->id;
        $result['pregunta'] = utf8_encode($faq->pregunta);
        $result['respuesta'] = utf8_encode($faq->respuesta);
        $result['uri'] = $faq->uri;
        $result = json_encode($result);
        header("Content-type: application/json");
        echo($result);
    }
    
    public function addVisitante($id) {
        $faq = Doctrine::getTable('faq')->find($id);
        $faq->visitas++;
        $faq->save();
    }
}

?>
